<?php

/**
 * This is the model class for table "matter".
 *
 * The followings are the available columns in table 'matter':
 * @property integer $id_matter
 * @property string $name
 * @property string $code
 * @property string $note
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class Matter extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Matter the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'matter';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('del_flg', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('code', 'length', 'max'=>16),
			array('note, date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_matter, name, code, note', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'ab' => array(self::HAS_MANY, 'Attribute', 'id_matter'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_matter' => '#',
			'name' => Yii::t('app', 'Name'),
			'code' => Yii::t('app', 'Code'),
			'note' => Yii::t('app', 'Note'),
			'date_add' => Yii::t('app', 'Date Add'),
			'date_upd' => Yii::t('app', 'Date Update'),
			'del_flg' => 'Del Flag',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
		$criteria->compare('id_matter',$this->id_matter);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('note',$this->note,true);


		return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    /**
     * get all matter to array
     * @return array
     */
    public static function getListMatter(){
        // get list Matter to select
        $matterObj = Matter::model()->findAll('del_flg = 0');
        $matterList = array();
        if ($matterObj) {
            foreach ($matterObj as $val) {
                $matterList[$val->id_matter] = $val->name;
            }
        }

        return $matterList;
    }
}